package com.leyou.auth.task;

import com.leyou.auth.config.JwtProperties;
import com.leyou.common.auth.entity.AppInfo;
import com.leyou.common.auth.entity.PayLoad;
import com.leyou.common.auth.utils.JwtUtils;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/16 0016
 * Time: 15:53
 * 定时任务
 */
@Slf4j
@Getter
@Component
public class PrivilegeTokenHolder {
    @Autowired
    private JwtProperties jwtProp;
    private String token;

    private static final long TOKEN_REFRESH_INTERVAL = 86400000L;

    @Scheduled(fixedDelay = TOKEN_REFRESH_INTERVAL)
    private void loadToken() {
        try {
            // 自己生成token
            AppInfo info = new AppInfo();
            info.setId(jwtProp.getPrivilege().getId());
            info.setServiceName(jwtProp.getPrivilege().getSecret());
            this.token = JwtUtils.generateTokenExpireInSeconds(info, jwtProp.getPrivateKey(), jwtProp.getPrivilege().getExpire());
            log.info("加载token成功,时间: {}", LocalDateTime.now().toString());
        } catch (Exception e) {
            throw new RuntimeException("加载token失败!", e);
        }
    }

}
